var gulp = require('gulp'),
  useref = require('gulp-useref'),
  uglify = require('gulp-uglify'),
  csso = require('gulp-csso'),
  filter = require('gulp-filter'),
  RevAll = require('gulp-rev-all'),
  rename = require('gulp-rename'),
  concat = require('gulp-concat'),
  sourcemaps = require('gulp-sourcemaps'),
  replace = require('gulp-replace'),
  sass = require('gulp-sass'),
  clean = require('gulp-clean'),
  babel = require('gulp-babel');

const TARGET_DIR = './dist';
const RESOURCES_DIR = './resources';

gulp.task('default', ['babel-js', 'sass', 'pkg-html', 'copy-fonts', 'pkg-frame', 'copy-js', 'copy-laycss']);

// 打包html
gulp.task('pkg-html', function () {
  var jsFilter = filter('**/*.js', {restore: true}),
    cssFilter = filter('**/*.css', {restore: true}),
    htmlFilter = filter(['**/*.html'], {restore: true});
  gulp.src(['*.html', 'pages/**/*.html'])
    .pipe(useref())                         // 解析html中的构建块
    .pipe(jsFilter)                         // 过滤所有js
    .pipe(sourcemaps.init())
    .pipe(uglify())                         // 压缩js
    .pipe(rename({pathname: ''}))
    .pipe(jsFilter.restore)
    .pipe(cssFilter)                        // 过滤所有css
    .pipe(csso())                           // 压缩优化css
    .pipe(cssFilter.restore)
    .pipe(RevAll.revision({                 // 生成版本号
      dontRenameFile: ['.html'],          // 不给 html 文件添加版本号
      dontUpdateReference: ['.html']      // 不给文件里链接的html加版本号
    }))
    .pipe(htmlFilter)                       // 过滤所有html
    .pipe(replace('../', ''))
    .pipe(replace('static/', '/layout/static/'))
    .pipe(htmlFilter.restore)
    .pipe(sourcemaps.write('./'))
    .pipe(replace('resources/frame/', '/layout/static/js/'))
    .pipe(replace('resources/js/', '/layout/static/js/'))
    .pipe(gulp.dest(TARGET_DIR))
});

// move font
gulp.task('copy-fonts', function () {
  gulp.src(RESOURCES_DIR + '/**/font/*.*(eot|svg|ttf|woff)')
    .pipe(rename({dirname: ''}))
    .pipe(gulp.dest(TARGET_DIR + '/static/font'))
});

// move css
gulp.task('copy-laycss', function () {
  gulp.src(RESOURCES_DIR + '/css/modules/**/*.*')
    .pipe(gulp.dest(TARGET_DIR + '/static/css/modules'))
});

// 把 frame里面的那一堆js移进dist
gulp.task('pkg-frame', function () {
  gulp.src('./resources/frame/*.js')
  // .pipe(concat('frame_lib.js'))
    .pipe(sourcemaps.init())
    .pipe(uglify())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest(TARGET_DIR + '/static/js'))
});

// sass
gulp.task('sass', function () {
  gulp.src('./resources/css/*.scss')
    .pipe(sourcemaps.init())
    .pipe(sass().on('error', sass.logError))
    .pipe(concat('bundle.css'))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./resources/css'))
});

gulp.task('watch-sass', function () {
  gulp.watch('./resources/css/*.scss', ['sass'])
});

gulp.task('babel-js', function () {
  gulp.src('./src/js/**/*.js')
    .pipe(sourcemaps.init())
    .pipe(babel({
      presets: ['env']
    }))
    .pipe(concat('bundle.js'))
    .pipe(uglify())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./resources/js'))
});

// move font
gulp.task('copy-js', function () {
  gulp.src(RESOURCES_DIR + '/js/*.*')
    .pipe(rename({dirname: ''}))
    .pipe(gulp.dest(TARGET_DIR + '/static/js'))
});

gulp.task('clean', function () {
  gulp.src(TARGET_DIR, {read: false})
    .pipe(clean())
});
